﻿using System;
using System.Collections.Generic;
using System.IO;

namespace YArchitech.Plumbing
{
	internal class HYHydrantMatrix
	{
		public HYHydrantMatrix()
		{
			this.m_i = 0;
			this.m_icur = 0;
		}

		public HYHydrantMatrix(int i)
		{
			this.m_i = i;
			this.m_icur = 0;
			this.m_GAMartrix.Clear();
			this.m_GRezult.Clear();
			for (int j = 0; j < i; j++)
			{
				this.m_GAMartrix.Add(new HYHydrantGroup());
				this.m_GRezult.Add(0.0);
			}
		}

		public bool Calculate()
		{
			if (this.m_icur != this.m_i)
			{
				return false;
			}
			for (int i = 0; i < this.m_i; i++)
			{
				if (!this.IsRightLine(i))
				{
					return false;
				}
				this.m_GAMartrix[i] = this.m_GAMartrix[i] / this.m_GAMartrix[i].GetAt(i);
				for (int j = i + 1; j < this.m_i; j++)
				{
					if (this.IsRightSubLine(j, i))
					{
						this.m_GAMartrix[j].Substract(i, this.m_GAMartrix[i]);
					}
				}
			}
			for (int i = this.m_i - 1; i >= 0; i--)
			{
				this.m_GRezult[i] = this.GetCur(i);
			}
			return true;
		}

		public bool SetGroupItem(int i, HYHydrantGroup group)
		{
			if (i > this.m_i || group.GetSize() != this.m_i + 1)
			{
				return false;
			}
			this.m_GAMartrix[i] = new HYHydrantGroup(group);
			this.m_icur++;
			return true;
		}

		public bool ModifyGroupItem(int i, HYHydrantGroup group)
		{
			if (i > this.m_i || group.GetSize() != this.m_i + 1 || i < 0)
			{
				return false;
			}
			this.m_GAMartrix[i] = new HYHydrantGroup(group);
			return true;
		}

		private bool IsRightLine(int i)
		{
			if (Math.Abs(this.m_GAMartrix[i].GetAt(i)) > 0.0)
			{
				return true;
			}
			for (int j = i; j < this.m_i; j++)
			{
				if (Math.Abs(this.m_GAMartrix[j].GetAt(i)) > 0.0)
				{
					HYHydrantGroup src = this.m_GAMartrix[i];
					this.m_GAMartrix[i] = new HYHydrantGroup(this.m_GAMartrix[j]);
					this.m_GAMartrix[j] = new HYHydrantGroup(src);
					return true;
				}
			}
			return false;
		}

		private bool IsRightSubLine(int j, int i)
		{
			return Math.Abs(this.m_GAMartrix[j].GetAt(i)) > 0.0;
		}

		private double GetCur(int i)
		{
			double num = 0.0;
			for (int j = this.m_i - 1; j > i; j--)
			{
				num -= this.m_GAMartrix[i].GetAt(j) * this.m_GRezult[j];
			}
			return num + this.m_GAMartrix[i].GetAt(this.m_i);
		}

		public void Print()
		{
			StreamWriter streamWriter = new StreamWriter("C:\\a.txt");
			foreach (HYHydrantGroup hyhydrantGroup in this.m_GAMartrix)
			{
				string text = "";
				foreach (double num in hyhydrantGroup.m_DAGroup)
				{
					text = text + "----" + num.ToString();
				}
				text += "\r\n";
				streamWriter.Write(text);
			}
			streamWriter.Close();
		}

		private int m_icur;

		public int m_i;

		public List<HYHydrantGroup> m_GAMartrix = new List<HYHydrantGroup>();

		public List<double> m_GRezult = new List<double>();
	}
}
